package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.CheckResult;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.ManagerTask;
import com.yjhb.android.sbz.enums.TaskStatus;
import com.yjhb.android.sbz.fragment.check.CheckListFragment;
import com.jojo.android.zxlib.adapter.FragmentAdapter;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.mvp.BasePresenter;
import com.jojo.android.zxlib.tool.ZxTool;
import com.jojo.android.zxlib.view.checkbox.SmoothCheckBox;
import com.qmuiteam.qmui.widget.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/15 14:53
 */
public class TaskCheckListActivity extends BaseActivity {
    private final String TAG = "TaskCheckListActivity";

    @BindView(R.id.tabLayout)
    QMUITabSegment tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.oneClickPass_tv)
    TextView oneClickPass_tv;
    @BindView(R.id.oneClickPass_layout)
    View oneClickPass_layout;
    @BindView(R.id.selectAll)
    View selectAll;
    @BindView(R.id.selectAll_tv)
    TextView selectAll_tv;
    @BindView(R.id.selectedCheckBox)
    SmoothCheckBox selectedCheckBox;
    @BindView(R.id.title_right_tv)
    TextView title_right_tv;

    List<BaseFragment> fragments = new ArrayList<>();
    private CheckListFragment checkingFragment;
    private CheckListFragment checkPassFragment;
    private CheckListFragment checkFailFragment;

    private int taskId;
    private int unCheckNum;
    private ManagerTask managerTask;
    private boolean isEdit;

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_task_check_list;
    }

    @Override
    protected void initUI() {
        managerTask = (ManagerTask) getIntent().getSerializableExtra("managerTask");
        if (managerTask != null) {
            taskId = managerTask.getTask().getId();
            unCheckNum = managerTask.getWaitAuditCount();
        }
        initFragments();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onBackPressed() {
        if (isEdit) {
            switchEdit();
        } else {
            finish();
        }
    }

    @Override
    protected void regEvent() {
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //只有在待处理的编辑模式下才显示
                switch (position) {
                    case 0:
                        if (isEdit) {
                            title_right_tv.setText("放弃");
                        } else {
                            title_right_tv.setText("批量处理");
                        }
                        title_right_tv.setVisibility(View.VISIBLE);
                        oneClickPass_layout.setVisibility(isEdit ? View.VISIBLE : View.GONE);
                        break;
                    case 1:
                        title_right_tv.setText("导出数据");
                        title_right_tv.setVisibility(View.VISIBLE);
                        oneClickPass_layout.setVisibility(View.GONE);
                        break;
                    case 2:
                        title_right_tv.setVisibility(View.GONE);
                        oneClickPass_layout.setVisibility(View.GONE);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //注销掉本身的选中功能
        selectedCheckBox.setOnClickListener(null);
        selectedCheckBox.setClickable(false);
        selectedCheckBox.setFocusable(false);
        //然后为父布局整体设置选中
        selectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean b = !selectedCheckBox.isChecked();
                selectedCheckBox.setChecked(b, false);
                if (checkingFragment != null) {
                    checkingFragment.selectedAll(b);
                }
            }
        });
        oneClickPass_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit && checkingFragment != null) {
                    checkingFragment.submit();
                }
            }
        });
    }

    @Override
    public void onEventMsg(EventMsg msg) {
        super.onEventMsg(msg);
        if (msg != null) {
            if (msg.msg == EventMsg.TASK_CHECK_BATCH_SUCCESS) {
                switchEdit(false);
            } else if (msg.msg == EventMsg.TASK_CHECK_SELECT_UPDATE) {
                update();
            }
        }
    }

    private void initFragments() {
        fragments.clear();
        fragments.add(checkingFragment = new CheckListFragment());
        checkingFragment.setTitle("待审核");
        checkingFragment.setTaskId(taskId);
        checkingFragment.setState(TaskStatus.TASK_STATUS_SUBMIT.getStatus());
        fragments.add(checkPassFragment = new CheckListFragment());
        checkPassFragment.setTitle("已通过");
        checkPassFragment.setTaskId(taskId);

        checkPassFragment.setState(TaskStatus.TASK_STATUS_PASS.getStatus());
        fragments.add(checkFailFragment = new CheckListFragment());
        checkFailFragment.setTitle("未通过");
        checkFailFragment.setTaskId(taskId);
        checkFailFragment.setState(TaskStatus.TASK_STATUS_FAIL.getStatus());
        viewpager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragments));

        tabLayout.setDefaultNormalColor(getResources().getColor(R.color.gray_66));
        tabLayout.setDefaultSelectedColor(getResources().getColor(R.color.colorPrimaryDark));
        tabLayout.setTabTextSize(ZxTool.dip2px(14));
        tabLayout.setupWithViewPager(viewpager);
    }

    private void update() {
        if (oneClickPass_tv != null) {
            Map<Long, CheckResult> map = checkingFragment.getSelectedMap();
            int count = 0;
            if (map != null) {
                count = map.size();
                if (count == checkingFragment.getCount()) {
                    if (count == 0){
                        selectedCheckBox.setChecked(false, false);
                        selectAll_tv.setText("全选");
                    }else {
                        selectedCheckBox.setChecked(true, false);
                        selectAll_tv.setText("全不选");
                    }
                } else {
                    selectedCheckBox.setChecked(false, false);
                    selectAll_tv.setText("全选");
                }
            } else {
                selectedCheckBox.setChecked(false, false);
                selectAll_tv.setText("全选");
            }
            oneClickPass_tv.setText("通过选中项（" + count + "）");
//            oneClickPass_tv.setText("通过选中项");
        }
    }

    private void switchEdit(boolean b) {
        isEdit = b;
        if (title_right_tv != null) {
            title_right_tv.setText(isEdit ? "放弃" : "批量处理");
        }
        if (oneClickPass_layout != null)
            oneClickPass_layout.setVisibility(isEdit ? View.VISIBLE : View.GONE);
        if (checkingFragment != null) {
            checkingFragment.setEdit(isEdit);
        }
    }

    private void switchEdit() {
        switchEdit(!isEdit);
    }

    @OnClick(R.id.title_right_tv)
    void rightTv() {
        int index = viewpager.getCurrentItem();
        if (index == 0) {
            switchEdit();
        } else if (index == 1) {
            Intent intent = new Intent(this, ExportDataActivity.class);
            intent.putExtra("managerTask",managerTask);
            startActivity(intent);
        }
    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }
}
